import { defineStore } from 'pinia'

interface AppState {
  // 主题设置
  isDarkMode: boolean
  
  // 布局设置
  sidebarCollapsed: boolean
  
  // 用户偏好
  readingMode: 'normal' | 'focus'
  fontSize: 'small' | 'medium' | 'large'
  
  // 应用状态
  isLoading: boolean
  error: string | null
  
  // 实时数据
  onlineUsers: number
  totalViews: number
}

export const useAppStore = defineStore('app', {
  state: (): AppState => ({
    isDarkMode: true, // 默认暗色主题
    sidebarCollapsed: false,
    readingMode: 'normal',
    fontSize: 'medium',
    isLoading: false,
    error: null,
    onlineUsers: Math.floor(Math.random() * 1000) + 500,
    totalViews: Math.floor(Math.random() * 1000000) + 500000
  }),

  getters: {
    // 获取当前主题类名
    themeClass: (state) => state.isDarkMode ? 'dark' : 'light',
    
    // 格式化在线用户数
    formattedOnlineUsers: (state) => {
      if (state.onlineUsers >= 1000) {
        return (state.onlineUsers / 1000).toFixed(1) + 'k'
      }
      return state.onlineUsers.toString()
    },
    
    // 格式化总浏览量
    formattedTotalViews: (state) => {
      if (state.totalViews >= 1000000) {
        return (state.totalViews / 1000000).toFixed(1) + 'M'
      } else if (state.totalViews >= 1000) {
        return (state.totalViews / 1000).toFixed(1) + 'k'
      }
      return state.totalViews.toString()
    }
  },

  actions: {
    // 切换主题
    toggleTheme() {
      this.isDarkMode = !this.isDarkMode
      this.saveToLocalStorage()
    },

    // 切换侧边栏
    toggleSidebar() {
      this.sidebarCollapsed = !this.sidebarCollapsed
    },

    // 设置阅读模式
    setReadingMode(mode: 'normal' | 'focus') {
      this.readingMode = mode
      this.saveToLocalStorage()
    },

    // 设置字体大小
    setFontSize(size: 'small' | 'medium' | 'large') {
      this.fontSize = size
      this.saveToLocalStorage()
    },

    // 设置加载状态
    setLoading(loading: boolean) {
      this.isLoading = loading
    },

    // 设置错误信息
    setError(error: string | null) {
      this.error = error
    },

    // 更新实时数据
    updateRealtimeData() {
      // 模拟实时数据更新
      this.onlineUsers = Math.floor(Math.random() * 1000) + 500
      this.totalViews += Math.floor(Math.random() * 10) + 1
    },

    // 保存设置到本地存储
    saveToLocalStorage() {
      const settings = {
        isDarkMode: this.isDarkMode,
        readingMode: this.readingMode,
        fontSize: this.fontSize
      }
      localStorage.setItem('tech-news-settings', JSON.stringify(settings))
    },

    // 从本地存储加载设置
    loadFromLocalStorage() {
      try {
        const settings = localStorage.getItem('tech-news-settings')
        if (settings) {
          const parsed = JSON.parse(settings)
          this.isDarkMode = parsed.isDarkMode ?? true
          this.readingMode = parsed.readingMode ?? 'normal'
          this.fontSize = parsed.fontSize ?? 'medium'
        }
      } catch (error) {
        console.error('加载设置失败:', error)
      }
    },

    // 初始化应用
    async initializeApp() {
      this.setLoading(true)
      try {
        // 加载用户设置
        this.loadFromLocalStorage()
        
        // 启动实时数据更新
        this.startRealtimeUpdates()
        
        console.log('应用初始化完成')
      } catch (error) {
        console.error('应用初始化失败:', error)
        this.setError('应用初始化失败')
      } finally {
        this.setLoading(false)
      }
    },

    // 启动实时数据更新
    startRealtimeUpdates() {
      // 每30秒更新一次实时数据
      setInterval(() => {
        this.updateRealtimeData()
      }, 30000)
    }
  }
})
